package com.hup.utils.swing.customView;

import javax.swing.JScrollPane;
import java.awt.Component;
import java.awt.Dimension;

/**
 * 优化过的 {@link JScrollPane};
 * .修复内容尺寸超出parent后,整个滚动面板无尺寸的问题;╮(╯_╰)╭
 * .加入增强GUI Designer的set/get方法
 * .默认状态时,是专用于横向滚动的panel
 *
 * @author hugan
 * @date 2020/5/19
 */
public class HScrollPanel extends JScrollPane {

    public HScrollPanel() {
        /*
        去掉边框
        直接new JScrollPane时是不会有边框的,但继承JScrollPane后,会出现边框;
         */
        setBorder(null);
        setHorizontalScrollUnit(30);
        setHorizontalScrollSize(new Dimension(-1, 3));
        setVerticalScrollUnit(30);
        setVerticalScrollSize(new Dimension(3, -1));
    }

    public HScrollPanel(Component c) {
        this();
        setViewportView(c);
    }

    /**
     * 被添加到parent后触发的回调?
     */
    @Override
    public void addNotify() {
        super.addNotify();
        //ShareLogger.info("size={}", getPreferredSize());
        setMinimumSize(getPreferredSize());
    }

    /**
     * 设置滚动条尺寸
     */
    public void setHorizontalScrollSize(Dimension size) {
        getHorizontalScrollBar().setPreferredSize(size);
    }

    public Dimension getHorizontalScrollSize() {
        return getHorizontalScrollBar().getPreferredSize();
    }

    /**
     * 设置滚动条尺寸
     */
    public void setVerticalScrollSize(Dimension size) {
        getVerticalScrollBar().setPreferredSize(size);
    }

    public Dimension getVerticalScrollSize() {
        return getVerticalScrollBar().getPreferredSize();
    }

    /**
     * 设置滚动距离
     *
     * @param val 单位,未知!!
     */
    public void setHorizontalScrollUnit(int val) {
        getHorizontalScrollBar().setUnitIncrement(val);
    }

    public int getHorizontalScrollUnit() {
        return getHorizontalScrollBar().getUnitIncrement();
    }

    public void setVerticalScrollUnit(int val) {
        getVerticalScrollBar().setUnitIncrement(val);
    }

    public int getVerticalScrollUnit() {
        return getVerticalScrollBar().getUnitIncrement();
    }

}
